package com.stylefeng.guns.modular.skate.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.base.controller.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.StringOperationUtils;
import com.stylefeng.guns.core.util.ToolUtil;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.model.Skate;
import com.stylefeng.guns.modular.skate.service.ISkateService;
import com.stylefeng.guns.modular.skate.service.impl.SkateServiceImpl;
import com.stylefeng.guns.modular.system.dao.SkateDao;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.DataRegion;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;

/**
 * 轮滑进销存储控制器
 *
 * @author fengshuonan
 * @Date 2020-12-20 01:08:31
 */
@Controller
@RequestMapping("/skate")
public class SkateController extends BaseController {

	private String PREFIX = "/skate/skate/";

	@Autowired
	private ISkateService skateService;

	@Autowired
	private SkateDao skateDao;
	
	@Autowired
	private SkateServiceImpl skateServiceImpl;

	/**
	 * 跳转到轮滑进销存储首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "skate.html";
	}

	/**
	 * 跳转到添加轮滑进销存储
	 */
	@RequestMapping("/skate_add")
	public String skateAdd() {
		return PREFIX + "skate_add.html";
	}

	/**
	 * 跳转到修改轮滑进销存储
	 */
	@RequestMapping("/skate_update/{skateId}")
	public String skateUpdate(@PathVariable Integer skateId, Model model) {
		Skate skate = skateService.selectById(skateId);
		model.addAttribute("item", skate);
		Map<String, String> fileMap = new HashMap<String, String>();
		if(skate.getFileUrl()!=null && skate.getFileUrl()!=""){
			fileMap=StringOperationUtils.dealWithJsonStrToMap(skate.getFileUrl());
		}
		model.addAttribute("fileMap", fileMap);
		LogObjectHolder.me().set(skate);
		return PREFIX + "skate_edit.html";
	}

	/**
	 * 获取轮滑进销存储列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(@RequestParam(required = false) String name,
			@RequestParam(required = false) Integer type,@RequestParam(required = false) String color,@RequestParam(required = false) Integer level) {
		Page<Map<String, Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
		Page<Map<String, Object>> backPage = skateServiceImpl.selectByCondition(page, name, type, color, level);
		return super.packForBT(backPage);
	}
	
	/**
	 * 导出轮滑进销存储列表
	 */
	@RequestMapping(value = "/export")
	@ResponseBody
	public String export(@RequestParam(required = false) String name,
			@RequestParam(required = false) Integer type,@RequestParam(required = false) String color,@RequestParam(required = false) Integer level) {
		Page<Map<String, Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
		Page<Map<String, Object>> backPage = skateServiceImpl.selectByCondition(page, name, type, color, level);
		//System.out.println(backPage.getRecords().toString());
		return null;
	}
	
	
	

	/**
	 * 新增轮滑进销存储
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(Skate skate) {
		System.out.println(skate.toString());
		skateService.insert(skate);
		return super.SUCCESS_TIP;
	}

	/**
	 * 删除轮滑进销存储
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Long skateId) {
		skateService.deleteById(skateId);
		return SUCCESS_TIP;
	}

	/**
	 * 修改轮滑进销存储
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(Skate skate) {
		skateService.updateById(skate);
		return super.SUCCESS_TIP;
	}

	/**
	 * 轮滑进销存储详情
	 */
	@RequestMapping(value = "/detail/{skateId}")
	@ResponseBody
	public Object detail(@PathVariable("skateId") Integer skateId) {
		return skateService.selectById(skateId);
	}
	
	@RequestMapping("/pageOffice")
	@ResponseBody
	public String pageOffice(Long id){
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
		poCtrl.setServerPage("/poserver.zz"); //此行必须
		poCtrl.addCustomToolButton("保存","Save",1); //添加自定义按钮
		poCtrl.setSaveFilePage("/skate/save");//设置保存的action
		WordDocument doc = new WordDocument();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		DataRegion dataRegion = doc.openDataRegion("PO_title");
		dataRegion.setValue("单车储物");
		poCtrl.setWriter(doc);
		poCtrl.setCustomToolbar(false);//隐藏自定义工具栏
		//打开文件/static/pageoffice/applicationForm.doc
		poCtrl.webOpen("d:\\applicationForm.doc", OpenModeType.docNormalEdit, ShiroKit.getUser().getName());
		System.out.println(poCtrl.getHtmlCode("PageOfficeCtrl1"));
		return poCtrl.getHtmlCode("PageOfficeCtrl1");
	}
	
	
	@RequestMapping("/save")
	public void saveFile(HttpServletRequest request, HttpServletResponse response){

	FileSaver fs = new FileSaver(request, response);
	
	System.out.println(fs.getFileName());

	fs.saveToFile("d:\\" + fs.getFileName());

	fs.close();

	}
	
}
